/*
 * 文字元素
 * 
 */

var ElementBase = require("./ElementBase.js");
var Matrix = require("./../tf/Matrix.js");

function ElementText() {
	//继承基类
	ElementBase.apply(this, arguments);

	this.type = "text";
	this._text = "";
	this._color = "#000";
	this._fontSize = 32;
	this._fontFamily = "";
	this._fontWeight = "";
	this._fontStyle = "";
	this._textAlign = "";
	this._path = "";
	this._baseline = "text-before-edge";
	this._bgColor = ""; //背景颜色
	this.dmatrix = new Matrix();
	
	Object.defineProperty(this, "baseline", {
		get: function() {
			return this._baseline;
		},
		set: function(value) {
			this._baseline = value;
			this.element.attr("dominant-baseline", value);
		},
		configurable: true
	});

	Object.defineProperty(this, "path", {
		get: function() {
			return this._path;
		},
		set: function(value) {
			this._path = value;
			if(!value) {
				this.element.attr({
					textpath: value
				});
				this.element.attr("text-anchor", "");
				this.baseline = "text-before-edge";
				this.element.attr("text-anchor", "");
			} else {
				this.element.attr({
					textpath: value
				});
				this.element.attr("text-anchor", "middle");
				this.element.textPath.attr("startOffset", "50%");
			}
			this.correctDMatrix();
		},
		configurable: true
	});

	/*
	 * 颜色设置
	 */
	Object.defineProperty(this, "color", {
		get: function() {
			return this._color;
		},
		set: function(value) {
			this._color = value;
			if(this.element) {
				this.element.attr("fill", this.color);
			}
		},
		configurable: true
	});

	/*
	 * 内容设置
	 */
	Object.defineProperty(this, "text", {
		get: function() {
			return this._text;
		},
		set: function(value) {
			value = $.trim(value);
			if(value == ""){
				value = "请输入文本";
			}
			this._text = value;
			if(this.element) {
				if(this.element.textPath){
					this.element.textPath.node.textContent = value;
				}else{
					this.element.node.textContent = value;
				}
				this.correctDMatrix();
			}
			Design.event.trigger(Design.event.ICON_UPDATE, this);
		},
		configurable: true
	});

	/*
	 * 文本大小
	 */
	Object.defineProperty(this, "fontSize", {
		get: function() {
			return this._fontSize;
		},
		set: function(value) {
			this._fontSize = value;
			if(this.element) {
				this.element.attr("fontSize", this._fontSize);
				this.correctDMatrix();
			}
		},
		configurable: true
	});

	/*
	 * 字体
	 */
	Object.defineProperty(this, "fontFamily", {
		get: function() {
			return this._fontFamily;
		},
		set: function(value) {
			this._fontFamily = value;
			if(this.element) {
				this.element.attr("fontFamily", this._fontFamily);
				this.correctDMatrix();
			}
		},
		configurable: true
	});

	/*
	 * 粗体
	 */
	Object.defineProperty(this, "fontWeight", {
		get: function() {
			return this._fontWeight;
		},
		set: function(value) {
			this._fontWeight = value;
			if(this.element) {
				this.element.attr("fontWeight", this._fontWeight);
				this.correctDMatrix();
			}
		},
		configurable: true
	});

	/*
	 * 斜体
	 */
	Object.defineProperty(this, "fontStyle", {
		get: function() {
			return this._fontStyle;
		},
		set: function(value) {
			this._fontStyle = value;
			if(this.element) {
				this.element.attr("fontStyle", this._fontStyle);
				this.correctDMatrix();
			}
		},
		configurable: true
	});

	/*
	 * 对齐
	 */
	Object.defineProperty(this, "textAlign", {
		get: function() {
			return this._textAlign;
		},
		set: function(value) {
			this._textAlign = value;
			if(this.element) {
				this.element.attr("textAlign", this._textAlign);
				this.correctDMatrix();
			}
		},
		configurable: true
	});
	
	Object.defineProperty(this, "bgColor", {
		get: function() {
			return this._bgColor;
		},
		set: function(value) {
			this._bgColor = value;
			if(value){
				this.element.attr("fill", this._bgColor);
			}else{
				this.element.attr("fill", "");
			}
		},
		configurable: true
	});
}

ElementText.prototype = new ElementBase();

/*
 * 设置图片信息
 */
ElementText.prototype.setJson = function($data) {
	this.super_setJson($data);

	this.element = Design.paper.text();
	this.element.attr("dominant-baseline", "text-before-edge");

	if($data.text) {
		this.text = $data.text;
	}else{
		this.text = "请输入文本";
	}
	if($data.fontSize) {
		this.fontSize = $data.fontSize;
	} else {
		this.fontSize = this.fontSize;
	}
	if($data.color) {
		this.color = $data.color;
	}
	if($data.fontFamily){
		this.fontFamily = $data.fontFamily;
	}
	if($data.fontWeight){
		this.fontWeight = $data.fontWeight;
	}
	if($data.fontStyle){
		this.fontStyle = $data.fontStyle;
	}
	if($data.textAlign){
		this.textAlign = $data.textAlign;
	}
	if($data.bgColor){
		this.bgColor = $data.bgColor;
	}
	if($data.path){
		this.path = $data.path;
	}
	if($data.baseline){
		this.baseline = $data.baseline;
	}
	this.appendToCon();
	this.createEnd();
}

/*
 * 修正 文本和变形工具间的 变换
 */
ElementText.prototype.correctDMatrix = function() {
	if(this.element) {
		var bbox = this.element.getBBox(true);
		this.dmatrix.x = bbox.x;
		this.dmatrix.y = bbox.y;
	}
	return this.dmatrix;
}

/*
 * 获取保存的数据
 */
ElementText.prototype.getSaveData = function() {
	var json = this.super_getSaveData();
	json.color = this.color;
	json.text = this.text;
	json.fontSize = this.fontSize;
	json.fontFamily = this.fontFamily;
	json.fontWeight = this.fontWeight;
	json.fontStyle = this.fontStyle;
	json.textAlign = this.textAlign;
	json.path = this.path;
	json.bgColor = this.bgColor;
	json.baseline = this.baseline;
	return json;
}

ElementText.prototype.getIcon = function(){
	return '<i class="iconfont icon-wenben icon-Text"></i><span>'+this.text+'</span>';
}

exports = module.exports = ElementText;